//
//  YMCheckGenerateReportAlert.swift
//  YMMedical
//
//  Created by y s on 2023/7/25.
//

import UIKit
import Lottie

@objcMembers public class YMCheckGenerateReportAlert: UIView {
    
    lazy var vAlert: UIView = {
        let result = UIView()
        result.backgroundColor = STheme.D9
        result.clipsToBounds = true
        result.layer.cornerRadius = SFitUtil.fit(8)
        return result
    }()
    lazy var vAnimation: LottieAnimationView = {
        let view = LottieAnimationView(name: "check_generate_report_image")
        view.loopMode = .loop
        view.contentMode = .scaleAspectFill
        return view
    }()

    lazy var lblTitle: UILabel = {
        let result = SThemeUtil.boldLabel(YMBridging.kLanguage("genrating_report_txt"), color: STheme.H_D1 , fontSize: 18)
        result.textAlignment = .center
        result.numberOfLines = 0
        return result
    }()
    lazy var lblSub: UILabel = {
        let result = SThemeUtil.label(YMBridging.kLanguage("report_loading_txt"), color: STheme.H_D3 , fontSize: 14)
        result.textAlignment = .center
        result.numberOfLines = 0
        return result
    }()
    
    public override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = SUIUtil.colorOf(0x000000, alpha: 0.5);
        self.frame = CGRectMake(0, 0, SFitUtil.screenWidth, SFitUtil.screenHeight)

        addSubview(vAlert)
        vAlert.addSubview(vAnimation)
        vAlert.addSubview(lblTitle)
        vAlert.addSubview(lblSub)
        vAlert.snp.makeConstraints { make in
            make.center.equalToSuperview()
            make.width.equalTo(SFitUtil.fit(330))
        }
        vAnimation.snp.makeConstraints { make in
            make.left.right.equalToSuperview()
            make.height.equalTo(SFitUtil.fit(216.5));
            make.top.equalTo(SFitUtil.fit(10));
        }
        lblTitle.snp.makeConstraints { make in
            make.left.equalTo(SFitUtil.fit(15));
            make.right.equalTo(SFitUtil.fit(-15));
            make.top.equalTo(vAnimation.snp.bottom).offset(SFitUtil.fit(3));
        }
        lblSub.snp.makeConstraints { make in
            make.left.equalTo(SFitUtil.fit(15));
            make.right.equalTo(SFitUtil.fit(-15));
            make.top.equalTo(lblTitle.snp.bottom).offset(SFitUtil.fit(15));
            make.bottom.equalTo(SFitUtil.fit(-39));
        }
        
        vAnimation.play()
    }
    
    func show() {
        let keyWindow = SThemeUtil.keyWindow()
        for i in 0 ..< keyWindow.subviews.count {
            if  keyWindow.subviews[i] is YMCheckGenerateReportAlert {
                return;
            }
        }
        keyWindow.addSubview(self)
        self.alpha = 0
        UIView.animate(withDuration: 0.3) {
            self.alpha = 1
        }
    }
    func dismiss() {
        DispatchQueue.main.async {[weak self] in
            self?.removeFromSuperview()
        }
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
}
